package com.lee.boot.head.first.demo.util;


import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
//import org.bson.io.OutputBuffer;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;


import java.io.*;
import java.nio.charset.Charset;

/**
 * @author: Lee
 * @Date: 2018/12/24 10:50
 * @Description:
 */
public class ImageToPdf {

    public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
        String imagePath = "C:\\Users\\Administrator\\Desktop\\A.jpg";
        String aimagePath = "C:\\Users\\Administrator\\Desktop\\andrew-wong-1244279-unsplash.jpg";
        String pDFPath = "C:\\Users\\Administrator\\Desktop\\j.pdf";
//        System.out.println(imgToPdf(AimagePath, PDFPath, imagePath));
        String html = "C:\\Users\\Administrator\\Desktop\\strategy-detail(1).html";

        htmlToPdf(html, pDFPath);
////        htmlToPdfV2(html, pDFPath);
//        htmlToPdfV3(html, pDFPath);

    }


    public static void htmlToPdf(String html, String pdfPath) throws IOException, DocumentException {
        // 会分页，类似word那样分页
        Document document = new Document(PageSize.A1);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));

        document.open();
        XMLWorkerHelper xmlWorkerHelper = XMLWorkerHelper.getInstance();
        xmlWorkerHelper.parseXHtml(pdfWriter, document, new FileInputStream(html), (Charset) null, new AsianFontProvider());
        document.close();
    }

    /**
     * xhtmlrenderertopdf
     *
     * @param htmlPath
     * @param pdfPath
     * @throws IOException
     * @throws com.lowagie.text.DocumentException
     */
    public static void htmlToPdfV2(String htmlPath, String pdfPath) throws IOException, com.lowagie.text.DocumentException {
        OutputStream outputStream = new FileOutputStream(pdfPath);
        ITextRenderer renderer = new ITextRenderer();
        String url = new File(htmlPath).toURI().toURL().toString();
        System.out.println(url);
        renderer.setDocument(url);
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont("C:\\Users\\Administrator\\Desktop\\simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        renderer.layout();
        renderer.createPDF(outputStream);
        outputStream.flush();
        outputStream.close();
    }

    /**
     *
     * @param htmlPath
     * @param pdfPath
     * @throws IOException
     */
    public static void htmlToPdfV3(String htmlPath, String pdfPath) throws IOException {
        FontProvider  font = new DefaultFontProvider(false, false, false);
        FontProgram font1 = FontProgramFactory.createFont("C:\\Users\\Administrator\\Desktop\\simsun.ttc,1");
        font.addFont(font1);
        ConverterProperties converterProperties = new ConverterProperties().setFontProvider(font);
        HtmlConverter.convertToPdf(new File(htmlPath),
                new File(pdfPath));
    }


    public static boolean imgToPdf(String imgFilePath, String pdfFilePath, String aImage) throws IOException, DocumentException {
        File file = new File(imgFilePath);
        if (file.exists()) {
            Document document = new Document();
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(pdfFilePath);
                PdfWriter.getInstance(document, fos);
                // 添加PDF文档的某些信息，比如作者，主题等等
//                document.addAuthor("Lee");
//                document.addSubject("test pdf.");
                // 设置PDF文档的大小

                // 打开文档
                document.open();
                // 写入一段文字
//                document.add(new Paragraph("JUST TEST ..."));
                // 读取一个图片
                Image image = Image.getInstance(imgFilePath);
                document.setPageSize(new Rectangle(3964.0F, 5946.0F));
                float imageHeight = image.getScaledHeight();
                float imageWidth = image.getScaledWidth();
                System.out.println(imageHeight);
                System.out.println(imageWidth);
                int i = 0;
//                while (imageHeight > 500 || imageWidth > 500) {
//                    System.out.println(imageHeight);
//                    System.out.println(imageWidth);
//                    image.scalePercent(100 - i);
//                    i++;
//                    imageHeight = image.getScaledHeight();
//                    imageWidth = image.getScaledWidth();
//                    System.out.println("imageHeight->" + imageHeight);
//                    System.out.println("imageWidth->" + imageWidth);
//                }
//                image.setAlignment(4);
                //设置图片的绝对位置
                image.setAbsolutePosition(0, 300);
//                image.scaleAbsolute(500, 550);
//                document.setPageSize(new Rectangle(image.getWidth(), image.getHeight()));
                // 插入一个图片
                document.add(image);
            } catch (DocumentException de) {
                System.out.println(de.getMessage());
            } catch (IOException ioe) {
                System.out.println(ioe.getMessage());
            }
            document.close();
            fos.flush();
            fos.close();
            // 设置水印图片
//            PdfReader pdfReader = new PdfReader(pdfFilePath);
//            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(aImage));
//            Image image = Image.getInstance(aImage);
//            //坐标
//            image.setAbsolutePosition(-100, 0);
//            //自定义大小
//            image.scalePercent(50);
////            image.scaleAbsolute(800, 1000);
//            PdfGState gs = new PdfGState();
//            // 设置透明度为0.2
//            gs.setFillOpacity(0.2f);
//            PdfContentByte underContent = pdfStamper.getUnderContent(1);
//            underContent.setGState(gs);
//            underContent.addImage(image);
            return true;
        } else {
            return false;
        }
    }
}
